import pandas as pd
from difflib import SequenceMatcher
from pathlib import Path

def check_redundant_papers(data_dir, verbose=False):
    """
    Check for redundant papers in the paper_info.csv file.
    
    Args:
        data_dir (str or Path): Directory containing the paper_info.csv file.
        verbose (bool): If True, print detailed output.
    """
    data_dir = Path(data_dir)
    
    # Load the CSV file
    try:
        df = pd.read_csv(data_dir / 'paper_info.csv')
    except FileNotFoundError:
        print(f"[ERROR] '{data_dir / 'paper_info.csv'}' not found in the data directory.")
        return
    except Exception as e:
        print(f"[ERROR] Failed to load CSV: {str(e)}")
        return

    print("\n=== Checking for Redundant Papers ===")
    
    # Run all checks
    check_duplicate_ids(df)
    check_duplicate_titles(df)
    check_similar_abstracts(df, similarity_threshold=0.85, show_abstracts=verbose)

    print("\n=== Redundancy Check Complete ===")

def check_duplicate_ids(df):
    """Check for duplicate paper IDs."""
    duplicate_ids = df[df.duplicated('id', keep=False)]
    if not duplicate_ids.empty:
        print("\n[!] Duplicate IDs found:")
        print(duplicate_ids[['id', 'title']].to_string(index=False))
    else:
        print("\n[✓] No duplicate IDs found.")

def check_duplicate_titles(df):
    """Check for duplicate paper titles."""
    duplicate_titles = df[df.duplicated('title', keep=False)]
    if not duplicate_titles.empty:
        print("\n[!] Duplicate titles found:")
        print(duplicate_titles[['id', 'title']].to_string(index=False))
    else:
        print("\n[✓] No duplicate titles found.")

def check_similar_abstracts(df, similarity_threshold=0.85, show_abstracts=False):
    """Check for similar abstracts using fuzzy string matching."""
    similar_pairs = []
    abstracts = df['abstract'].fillna('').astype(str)  # Handle NaN values
    
    for i in range(len(df)):
        for j in range(i + 1, len(df)):
            # Skip if either abstract is empty
            if not abstracts[i] or not abstracts[j]:
                continue
                
            similarity = SequenceMatcher(None, abstracts[i], abstracts[j]).ratio()
            if similarity > similarity_threshold:
                similar_pairs.append((
                    df.iloc[i]['id'], 
                    df.iloc[j]['id'],
                    df.iloc[i]['title'],
                    df.iloc[j]['title'],
                    similarity,
                    abstracts[i],
                    abstracts[j]
                ))

    if similar_pairs:
        print("\n[!] Similar abstracts found (threshold = {}):".format(similarity_threshold))
        for pair in similar_pairs:
            print(f"\nPapers {pair[0]} and {pair[1]} (similarity: {pair[4]:.2f})")
            print(f"Title 1: {pair[2]}")
            print(f"Title 2: {pair[3]}")
            
            if show_abstracts:
                print("\nAbstract 1:")
                print(pair[5])
                print("\nAbstract 2:")
                print(pair[6])
                print("-" * 80)
    else:
        print("\n[✓] No similar abstracts found (threshold = {}).".format(similarity_threshold))